<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="景点名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="景点名称"></el-input>
    </el-form-item>
    <el-form-item label="景点类型id" prop="typeid">
      <el-input v-model="dataForm.typeid" placeholder="景点类型id"></el-input>
    </el-form-item>
    <el-form-item label="景点类型名称" prop="typename">
      <el-input v-model="dataForm.typename" placeholder="景点类型名称"></el-input>
    </el-form-item>
    <el-form-item label="景点介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="景点介绍"></el-input>
    </el-form-item>
    <el-form-item label="景点图片" prop="imgs">
      <el-input v-model="dataForm.imgs" placeholder="景点图片"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="流行度" prop="prevalence">
      <el-input v-model="dataForm.prevalence" placeholder="流行度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          typeid: '',
          typename: '',
          introduce: '',
          imgs: '',
          longitude: '',
          latitude: '',
          prevalence: ''
        },
        dataRule: {
          name: [
            { required: true, message: '景点名称不能为空', trigger: 'blur' }
          ],
          typeid: [
            { required: true, message: '景点类型id不能为空', trigger: 'blur' }
          ],
          typename: [
            { required: true, message: '景点类型名称不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '景点介绍不能为空', trigger: 'blur' }
          ],
          imgs: [
            { required: true, message: '景点图片不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          prevalence: [
            { required: true, message: '流行度不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/route/poi/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.poi.name
                this.dataForm.typeid = data.poi.typeid
                this.dataForm.typename = data.poi.typename
                this.dataForm.introduce = data.poi.introduce
                this.dataForm.imgs = data.poi.imgs
                this.dataForm.longitude = data.poi.longitude
                this.dataForm.latitude = data.poi.latitude
                this.dataForm.prevalence = data.poi.prevalence
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/route/poi/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'typeid': this.dataForm.typeid,
                'typename': this.dataForm.typename,
                'introduce': this.dataForm.introduce,
                'imgs': this.dataForm.imgs,
                'longitude': this.dataForm.longitude,
                'latitude': this.dataForm.latitude,
                'prevalence': this.dataForm.prevalence
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
